//
//  CPLoginVC.m
//  CPApp
//
//  Created by yobet on 2018/3/23.
//  Copyright © 2018年 Class. All rights reserved.
//

#import "CPLoginVC.h"
#import "CPRegisterVC.h"
#import "CPFindPasswordVC.h"
#import "AppDelegate.h"
#import "CPTabbarController.h"
#define MineSB [UIStoryboard storyboardWithName:@"Mine" bundle:nil]

@interface CPLoginVC ()
@property (weak, nonatomic) IBOutlet UITextField *account_TF;
@property (weak, nonatomic) IBOutlet UITextField *password_TF;

@end

@implementation CPLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [mNotificationCenter addObserver:self selector:@selector(registerUserNotification:) name:@"regsiterNotification" object:nil];
}

-(void)registerUserNotification:(NSNotification *)not {
    self.account_TF.text = not.userInfo[@"username"];
    self.password_TF.text = not.userInfo[@"password"];
    [self loginRequest];
}

- (IBAction)loginBtn:(UIButton *)sender {
    [self loginRequest];
}
-(void)loginRequest {
    if (self.account_TF.text.length<=0) {
        [ProgressHUD show:@"请输入账号"];
        return;
    }
    if (self.password_TF.text.length<6) {
        [ProgressHUD show:@"请输入密码"];
        return;
    }
    [AVUser logInWithUsernameInBackground:self.account_TF.text password:self.password_TF.text block:^(AVUser * _Nullable user, NSError * _Nullable error) {
        if (user !=nil) {
            NSLog(@"登录成功");
            [mUserDefaults setObject:@"1" forKey:@"login"];
            [mUserDefaults setObject:self.account_TF.text forKey:@"username"];
            [mUserDefaults setObject:self.password_TF.text forKey:@"password"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}
- (IBAction)registerBtn:(UIButton *)sender {
    CPRegisterVC *registerVC = [MineSB instantiateViewControllerWithIdentifier:@"Register"];
    registerVC.title = @"注册";
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (IBAction)editPassword:(UIButton *)sender {
    CPFindPasswordVC * findVC= [MineSB instantiateViewControllerWithIdentifier:@"FindPassword"];
    findVC.title = @"找回密码";
    [self.navigationController pushViewController:findVC animated:YES];
}

@end
